package com.cxh.rmsq.net.newNet

import android.text.TextUtils
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.cxh.rmsq.db.model.BaseResultInviteListBean
import com.cxh.rmsq.db.model.BaseResultJoinActivityListBean
import com.cxh.rmsq.db.model.BaseResultJoinRecordListBean
import com.cxh.rmsq.db.model.BaseResultListBean
import com.cxh.rmsq.db.model.BaseResultOrderListBean
import com.cxh.rmsq.db.model.BaseResultRankListBean
import com.cxh.rmsq.db.model.ClueBean
import com.cxh.rmsq.db.model.FriendShipInfo
import com.cxh.rmsq.db.model.HuntBean
import com.cxh.rmsq.db.model.InviteExchangeBean
import com.cxh.rmsq.db.model.MapMarkersBean
import com.cxh.rmsq.db.model.MapProgressBean
import com.cxh.rmsq.db.model.OrderListBean
import com.cxh.rmsq.db.model.PayOrderBean
import com.cxh.rmsq.db.model.ProgressBean
import com.cxh.rmsq.model.LoginResult
import com.cxh.rmsq.utils.toRequestBody
import kotlinx.coroutines.launch

class TreasureViewModel : ViewModel() {

    /**
     * 获得寻宝活动
     */
    fun getAppHunt(func: (BaseResultEntity<HuntBean>) -> Unit) {
        viewModelScope.launch {
            func(
                ApiServiceClient.apiService.getAppHunt()
            )
        }
    }

    /**
     * 获得寻宝活动
     */
    fun getExchangeRanking(
        pageNo: Int,
        pageSize: Int,
        func: (BaseResultEntity<BaseResultRankListBean>) -> Unit
    ) {
        viewModelScope.launch {
            func(
                ApiServiceClient.apiService.getExchangeRanking(pageNo, pageSize)
            )
        }
    }


    /**
     * 获得寻宝活动
     */
    fun getMapProgress(
        clueId: Int,
        func: (BaseResultEntity<MapProgressBean>) -> Unit
    ) {
        viewModelScope.launch {
            func(
                ApiServiceClient.apiService.getMapProgress(clueId)
            )
        }
    }


    /**
     * 获得寻宝活动
     */
    fun getHuntLocation(
        ids: Array<Int>,
        func: (BaseResultEntity<MutableList<MapMarkersBean>>) -> Unit
    ) {
        viewModelScope.launch {
            func(
                ApiServiceClient.apiService.getHuntLocation(ids)
            )
        }
    }

    /**
     * 获得寻宝活动
     */
    fun getClueList(
        locationId: String,
        func: (BaseResultEntity<ClueBean>) -> Unit
    ) {
        viewModelScope.launch {
            func(
                ApiServiceClient.apiService.getClueList(locationId)
            )
        }
    }

    /**
     * 获得是否寻宝签到
     */
    fun getUserCheckIn(
        locationId: String,
        func: (BaseResultEntity<Int>) -> Unit
    ) {
        viewModelScope.launch {
            func(
                ApiServiceClient.apiService.getUserCheckIn(locationId)
            )
        }
    }

    /**
     * 签到
     */
    fun createSign(
        data: Any,
        func: (BaseResultEntity<Any>) -> Unit
    ) {
        viewModelScope.launch {
            func(
                ApiServiceClient.apiService.createSign(toRequestBody(data))
            )
        }
    }

    /**
     * 签到
     */
    fun currentUserInviteCodePage(
        pageNo: Int,
        pageSize: Int,
        func: (BaseResultEntity<BaseResultInviteListBean>) -> Unit
    ) {
        viewModelScope.launch {
            func(
                ApiServiceClient.apiService.currentUserInviteCodePage(pageNo, pageSize)
            )
        }
    }

    /**
     * 询是否有未读邀请或分享线索
     */
    fun getClueUserCount(
        locationId: String,
        func: (BaseResultEntity<InviteExchangeBean>) -> Unit
    ) {
        viewModelScope.launch {
            func(
                ApiServiceClient.apiService.getClueUserCount(locationId)
            )
        }
    }


    /**
     * 询是否有未读邀请或分享线索
     */
    fun createExchange(
        data: Any,
        func: (BaseResultEntity<Any>) -> Unit
    ) {
        viewModelScope.launch {
            func(
                ApiServiceClient.apiService.createExchange(toRequestBody(data))
            )
        }
    }

    /**
     * 创建用户打卡记录
     */
    fun createDakaRecords(
        data: Any,
        func: (BaseResultEntity<Any>) -> Unit
    ) {
        viewModelScope.launch {
            func(
                ApiServiceClient.apiService.createDakaRecords(toRequestBody(data))
            )
        }
    }

    /**
     * 兑换记录
     */
    fun getExchangeRecords(
        completionStatus: Int, pageNo: Int, pageSize: Int,
        func: (BaseResultEntity<BaseResultJoinRecordListBean>) -> Unit
    ) {
        viewModelScope.launch {
            func(
                ApiServiceClient.apiService.getExchangeRecords(completionStatus, pageNo, pageSize)
            )
        }
    }

    /**
     * 兑换记录
     */
    fun getExchangeRecords2(
        completionStatus: Int, clueId: String, pageNo: Int, pageSize: Int,
        func: (BaseResultEntity<BaseResultJoinRecordListBean>) -> Unit
    ) {
        viewModelScope.launch {
            func(
                ApiServiceClient.apiService.getExchangeRecords2(
                    completionStatus,
                    clueId,
                    pageNo,
                    pageSize
                )
            )
        }
    }

    /**
     * 寻宝期数
     */
    fun getHuntTimeList(
        func: (BaseResultEntity<MutableList<HuntBean>>) -> Unit
    ) {
        viewModelScope.launch {
            func(
                ApiServiceClient.apiService.getHuntTimeList()
            )
        }
    }

    /**
     * 删除用户地图进度
     */
    fun getDeleteMapProgress(
        id: String, func: (BaseResultEntity<Any>) -> Unit
    ) {
        viewModelScope.launch {
            func(
                ApiServiceClient.apiService.getDeleteMapProgress(id)
            )
        }
    }

    /**
     * 创建藏宝订单
     */
    fun createOrder(
        data: Any, func: (BaseResultEntity<PayOrderBean>) -> Unit
    ) {
        viewModelScope.launch {
            func(
                ApiServiceClient.apiService.createOrder(toRequestBody(data))
            )
        }
    }

    /**
     * 删除藏宝订单
     */
    fun deleteTreasureOrder(
        id: String, func: (BaseResultEntity<Any>) -> Unit
    ) {
        viewModelScope.launch {
            func(
                ApiServiceClient.apiService.deleteTreasureOrder(id)
            )
        }
    }

    /**
     * 我的订单列表
     * payStatus: String,pageNo: Int,pageSize: Int
     */
    fun getOrderList(
        param2: String, pageNo: Int, pageSize: Int,

        func: (BaseResultEntity<BaseResultOrderListBean>) -> Unit
    ) {
        val paramsMap = HashMap<String, Any>()
        if (!TextUtils.isEmpty(param2)) {
            paramsMap["payStatus"] = param2
        }
        paramsMap["pageNo"] = pageNo
        paramsMap["pageSize"] = pageSize
        viewModelScope.launch {
            func(
                ApiServiceClient.apiService.getOrderList(param2, pageNo, pageSize)
            )
        }
    }

    /**
     * 获得藏宝订单
     */
    fun getOrderDetail(
        id: String,
        func: (BaseResultEntity<OrderListBean>) -> Unit
    ) {
        viewModelScope.launch {
            func(
                ApiServiceClient.apiService.getOrderDetail(id)
            )
        }
    }
    /**
     * 获得地图进度状态
     */
    fun getPayNum(
        id: String,
        func: (BaseResultEntity<ProgressBean>) -> Unit
    ) {
        viewModelScope.launch {
            func(
                ApiServiceClient.apiService.getPayNum(id)
            )
        }
    }


    /**
     * 更新用户地图进度
     */
    fun updateProgress(
        data: Any, func: (BaseResultEntity<Any>) -> Unit
    ) {
        viewModelScope.launch {
            func(
                ApiServiceClient.apiService.updateProgress(toRequestBody(data))
            )
        }
    }


    /**
     * 三方登录
     */
    fun loginThird(
        data: Any, func: (BaseResultEntity<LoginResult>) -> Unit
    ) {
        viewModelScope.launch {
            func(
                ApiServiceClient.apiService.loginThird(toRequestBody(data))
            )
        }
    }

    /**
     * 三方认证修改手机号
     */
    fun thirdBindMob(
        data: Any, func: (BaseResultEntity<LoginResult>) -> Unit
    ) {
        viewModelScope.launch {
            func(
                ApiServiceClient.apiService.thirdBindMob(toRequestBody(data))
            )
        }
    }

    /**
     * 三方认证修改手机号
     */
    fun qrCodeLogin(
        data: Any, func: (BaseResultEntity<LoginResult>) -> Unit
    ) {
        viewModelScope.launch {
            func(
                ApiServiceClient.apiService.qrCodeLogin(toRequestBody(data))
            )
        }
    }
}